package cn.schoolwow.workflow.module.task.action.flow.complete.nodeType.general;

import cn.schoolwow.quickdao.domain.transaction.Transaction;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceStatus;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.entity.WorkFlowNode;
import cn.schoolwow.workflow.module.common.flow.AddWorkFlowHistoryFlow;

public class HandleRejectNodeFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        WorkFlowNode nextNode = flowContext.checkData("nextNode", WorkFlowNode.class);
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        Transaction transaction = flowContext.checkInstanceData(Transaction.class);

        flowContext.startFlow(new AddWorkFlowHistoryFlow())
                .putCurrentCompositeFlowData("message", "自动拒绝节点["+nextNode.getName()+"]")
                .execute();

        transaction.addRecordString("执行自动拒绝节点");
        transaction.query(WorkFlowInstance.class)
                .addQuery("id", workFlowInstance.getId())
                .addQuery("definitionId", workFlowInstance.getDefinitionId())
                .addUpdate("contextData", workFlowInstance.getContextData())
                .addUpdate("meta", workFlowInstance.getMeta())
                .addUpdate("workFlowNodeId", nextNode.getId())
                .addUpdate("status", WorkFlowInstanceStatus.Reject.status)
                .execute()
                .update();
    }

    @Override
    public String name() {
        return "处理自动拒绝节点";
    }
}
